<?php

/*
 * 处理Meta表的类
 * 表结构：id，oid，key，value
 * $meta = new Meta();
 * $meta->table('xxx')->oid('xxx')->find()
 */
class Meta {
	/*
	 * 设置表名，表名为wx_table
	 */
	public function table($table) {
		$this->options['tableThinkPHP'] = $table;
		$this->options['table'] = C('DB_PREFIX').$this->options['tableThinkPHP'];
		return $this;
	}
	
	/*
	 * 设置表名通过模块名，表名为wx_module_<module>_meta
	*/
	public function module($module) {
		$this->options['tableThinkPHP'] = 'module_'.$module.'_meta';
		$this->options['table'] = C('DB_PREFIX').$this->options['tableThinkPHP'];
		return $this;
	}
		
	/*
	 * 设置表名通过实体名，表名为wx_entity_meta
	*/
	public function entity($entity) {
		$this->options['tableThinkPHP'] = $entity.'_meta';
		$this->options['table'] = C('DB_PREFIX').$this->options['tableThinkPHP'];
		return $this;
	}

	/*
	 * 设置oid，用于find/add/save/delete
	*/
	public function oid($oid) {
		$this->options['oid'] = $oid;
		return $this;
	}
		
	/*
	 * 设置value，用于add/save
	*/
	public function value($value) {
		$this->options['value'] = $value;
		return $this;
	}
	
	/*
	 * 获取某个对象的所有meta
	 * $this->table('xxx')->oid('xxx')->find()
	 */
	public function find() {
		if (!isset($this->options['table']) || !isset($this->options['oid'])) {
			throw_exception('没有设置表名/oid');
		}
		
		$metaModel = D($this->options['tableThinkPHP']);
		
		$where['oid'] = $this->options['oid'];
		$metas = $metaModel->where($where)->select();
		if (!isset($metas)) {
			return NULL;
		}
		
		return $metas;
	}
	
	/*
	 * 添加一个对象的所有属性到meta表
	 * $this->table('xxx')->oid('xxx')->value(value)->add()
	 */
	public function add() {
		if (!isset($this->options['table']) || !isset($this->options['oid']) || !isset($this->options['value'])) {
			throw_exception('没有设置表名/oid/value');
		}

		$sql = array();
		array_push($sql, 'INSERT INTO ' . $this->options['table'] . '(`oid`, `metaKey`, `metaValue`) VALUES ');
		foreach ($this->options['value'] as $key => $value ) {
			array_push($sql, "(" . $this->options['oid'] . ", '$key', '$value');");
		}
		$sql = implode("", $sql);
		$sql = str_replace(');(', '),(', $sql);
		
		$model = new Model();
		$model->query($sql);
	}
	
	/*
	 * 更新所有的meta，不存在的meta则新建
	 * $this->table('xxx')->oid('xxx')->value(value)->save()
	 */
	public function save() {
		if (!isset($this->options['table']) || !isset($this->options['oid']) || !isset($this->options['value'])) {
			throw_exception('没有设置表名/oid/value');
		}
		
		$has = $this->find();
		$exist = array();
		$add = array();
		foreach ($this->options['value'] as $metaKey => $metaValue) {
			foreach ($has as $hasMetaKey => $hasMetaValue) {
				if ($hasMetaKey == $hasMetaKey) {
					$exist[$metaKey] = $metaValue;
					break;
				}
			}
			
			if (!isset($exist[$metaKey])) {
				$add[$metaKey] = $metaValue;
			}
		}

		if (count($exist) > 0) {
			$sql = array();
			array_push($sql, 'UPDATE ' . $this->options['table'] . ' SET metaValue = CASE metaKey');
			foreach ($exist as $metaKey => $metaValue) {
				array_push($sql, " WHEN '$metaKey' THEN '$metaValue'");
			}
			$keys = "'".implode("','", array_keys($exist))."'";
			array_push($sql, " END WHERE metaKey IN ($keys)");
			$sql = implode("", $sql);
			
			$model = new Model();
			$model->query($sql);
		}
		
		if (count($add) > 0 ) {
			$this->options['value'] = (object)$add;
			$this->add();
		}	
	}	
	
	/*
	 * 删除某个oid的所有meta
	 * $this->table('xxx')->oid('xxx')->delete()
	 */
	public function delete() {
		if (!isset($this->options['table']) || !isset($this->options['oid'])) {
			throw_exception('没有设置表名/oid');
		}
		
		$metaModel = D($this->options['tableThinkPHP']);
		
		$where['oid'] = $this->options['oid'];
		$metaModel->where($where)->delete();
	}
	
	protected $options = array();
	
}